package com.jc.camerascan.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.baidu.mobstat.StatService;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import com.jc.camerascan.R;
import com.jc.camerascan.core.BaseActivity;
import com.jc.camerascan.fragment.mine.MineFragment;
import com.jc.camerascan.fragment.scan.ScanFragment;
import com.jc.camerascan.utils.XToastUtils;
import com.meituan.android.walle.WalleChannelReader;
import com.tencent.bugly.beta.Beta;
import com.xuexiang.xui.adapter.FragmentAdapter;
import com.xuexiang.xui.utils.StatusBarUtils;
import com.xuexiang.xutil.XUtil;
import com.xuexiang.xutil.common.ClickUtils;
import com.xuexiang.xutil.common.CollectionUtils;

import butterknife.BindView;

/**
 * @ProjectName: PhotoRecognition
 * @Package: com.jc.camerascan.activity
 * @ClassName: MainActivity
 * @Description: java类作用描述
 * @Author: XuChi
 * @CreateDate: 2020/7/8 17:27
 * @Version: 1.0
 */
public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener, BottomNavigationViewEx.OnNavigationItemSelectedListener, ClickUtils.OnClick2ExitListener {

    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.bottom_navigation)
    BottomNavigationViewEx bottomNavigation;

    String[] titles = new String[]{"首页", "我的"};

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main_new;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
//        Beta.checkUpgrade(false, false);
    }

    private void initView() {
        String channel = WalleChannelReader.getChannel(this.getApplicationContext());
        StatService.setAppChannel(getApplicationContext(), channel, true);
        StatService.start(this);
        StatusBarUtils.translucent(this);
        StatusBarUtils.setStatusBarLightMode(MainActivity.this);

        FragmentAdapter<Fragment> adapter = new FragmentAdapter<>(getSupportFragmentManager());
        for (String title : titles) {
            switch (title) {
                case "首页":
                    adapter.addFragment(new ScanFragment(), title);
                    break;
                case "我的":
                    adapter.addFragment(new MineFragment(), title);
                    break;
            }
        }
        viewPager.setOffscreenPageLimit(titles.length - 1);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(this);
        bottomNavigation.setOnNavigationItemSelectedListener(this);
        bottomNavigation.enableAnimation(false);
        bottomNavigation.enableShiftingMode(false);
        bottomNavigation.enableItemShiftingMode(false);
    }

    //=============ViewPager===================//

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        bottomNavigation.getMenu().getItem(position).setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //================Navigation================//

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int index = CollectionUtils.arrayIndexOf(titles, menuItem.getTitle());
        if (index != -1) {
            viewPager.setCurrentItem(index, true);
            return true;
        }
        return false;
    }

    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ClickUtils.exitBy2Click(2000, this);
        }
        return true;
    }

    @Override
    public void onRetry() {
        XToastUtils.toast("再按一次退出程序");
    }

    @Override
    public void onExit() {
        XUtil.exitApp();
    }
}
